Option Strict On
Option Explicit On

'Copyright (c) 2007-2010, William Severance, Jr., WESNet Designs
'All rights reserved.

'Redistribution and use in source and binary forms, with or without modification, are permitted
'provided that the following conditions are met:

'Redistributions of source code must retain the above copyright notice, this list of conditions
'and the following disclaimer.

'Redistributions in binary form must reproduce the above copyright notice, this list of conditions
'and the following disclaimer in the documentation and/or other materials provided with the distribution.

'Neither the name of William Severance, Jr. or WESNet Designs may be used to endorse or promote
'products derived from this software without specific prior written permission.

'Disclaimer: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
'            OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
'            AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER BE LIABLE
'            FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
'            LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
'            INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
'            OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
'            IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

'Although I will try to answer questions regarding the installation and use of this software when
'such questions are submitted via e-mail to the below address, no promise of further
'support or enhancement is made nor should be assumed.

'Developer Contact Information:
'     William Severance, Jr.
'     WESNet Designs
'     679 Upper Ridge Road
'     Bridgton, ME 04009
'     Phone: 207-647-9375
'     E-Mail: bill@wesnetdesigns.com
'     Website: www.wesnetdesigns.com

Imports System
Imports System.Configuration
Imports System.Data
Imports System.Xml.Serialization
Imports DotNetNuke.Entities.Modules

Namespace WESNet.DNN.Modules.EPrayer
    <Serializable(), XmlRoot("Request")> _
    Public Class RequestInfo
        Implements IHydratable

#Region "Private Members"

        'Fields corresponding to Requests table columns

        Private _RequestID As Integer = -1
        Private _ModuleID As Integer = -1
        Private _IsPraise As Boolean
        Private _CategoryID As Integer
        Private _Subject As String
        Private _SubjectContact As SubjectContactInfo
        Private _ChurchRelationshipID As Integer
        Private _StatusID As Integer
        Private _Expires As DateTime
        Private _IsLocked As Boolean
        Private _Views As Integer = 0
        Private _Updates As Integer
        Private _LastPostID As Integer
        Private _LastPostDate As DateTime

        'Fields looked up from IDs corresponding to Requests table columns

        Private _Category As String
        Private _ChurchRelationship As String
        Private _Status As String

        'Field looked up from UserID and RequestID to determine if currently logged in user is tracking updates
        'to this request

        Private _IsTracking As Boolean

        'Collection of posts for this request _Posts(0) is always the original request

        Private _Posts As Generic.List(Of PostInfo)

        'Constants for post cacheing

        Private PostCacheKey As String = "EPrayerPosts"
        Private PostCacheSeconds As Integer = 30

#End Region

#Region "Public Properties"

        <XmlIgnore()> _
        Public Property RequestID() As Integer
            Get
                Return _RequestID
            End Get
            Set(ByVal Value As Integer)
                _RequestID = Value
            End Set
        End Property

        <XmlIgnore()> _
        Public Property ModuleID() As Integer
            Get
                Return _ModuleID
            End Get
            Set(ByVal Value As Integer)
                _ModuleID = Value
            End Set
        End Property

        Public Property IsPraise() As Boolean
            Get
                Return _IsPraise
            End Get
            Set(ByVal value As Boolean)
                _IsPraise = value
            End Set
        End Property

        <XmlIgnore()> _
        Public Property CategoryID() As Integer
            Get
                Return _CategoryID
            End Get
            Set(ByVal value As Integer)
                _CategoryID = value
            End Set
        End Property

        Public Property Category() As String
            Get
                Return _Category
            End Get
            Set(ByVal value As String)
                _Category = value
            End Set
        End Property

        Public Property Subject() As String
            Get
                Return _Subject
            End Get
            Set(ByVal value As String)
                _Subject = value
            End Set
        End Property

        <XmlElement(IsNullable:=False)> _
        Public Property SubjectContact() As SubjectContactInfo
            Get
                Return _SubjectContact
            End Get
            Set(ByVal value As SubjectContactInfo)
                _SubjectContact = value
            End Set
        End Property

        <XmlIgnore()> _
        Public Property ChurchRelationshipID() As Integer
            Get
                Return _ChurchRelationshipID
            End Get
            Set(ByVal value As Integer)
                _ChurchRelationshipID = value
            End Set
        End Property

        Public Property ChurchRelationship() As String
            Get
                Return _ChurchRelationship
            End Get
            Set(ByVal value As String)
                _ChurchRelationship = value
            End Set
        End Property

        <XmlIgnore()> _
        Public Property StatusID() As Integer
            Get
                Return _StatusID
            End Get
            Set(ByVal value As Integer)
                _StatusID = value
            End Set
        End Property

        Public Property Expires() As DateTime
            Get
                Return _Expires
            End Get
            Set(ByVal value As DateTime)
                _Expires = value
            End Set
        End Property

        <XmlIgnore()> _
        Public ReadOnly Property IsExpired() As Boolean
            Get
                Return (Not _Expires = DateTime.MinValue) AndAlso (_Expires < Today())
            End Get
        End Property

        Public Property Status() As String
            Get
                Return _Status
            End Get
            Set(ByVal value As String)
                _Status = value
            End Set
        End Property

        Public Property IsLocked() As Boolean
            Get
                Return _IsLocked
            End Get
            Set(ByVal value As Boolean)
                _IsLocked = value
            End Set
        End Property

        Public Property Views() As Integer
            Get
                Return _Views
            End Get
            Set(ByVal value As Integer)
                _Views = value
            End Set
        End Property

        Public Property Updates() As Integer
            Get
                Return _Updates
            End Get
            Set(ByVal value As Integer)
                _Updates = value
            End Set
        End Property

        <XmlIgnore()> _
        Public Property LastPostID() As Integer
            Get
                Return _LastPostID
            End Get
            Set(ByVal value As Integer)
                _LastPostID = value
            End Set
        End Property

        Public Property LastPostDate() As DateTime
            Get
                Return _LastPostDate
            End Get
            Set(ByVal value As DateTime)
                _LastPostDate = value
            End Set
        End Property

        <XmlIgnore()> _
        Public Property IsTracking() As Boolean
            Get
                Return _IsTracking
            End Get
            Set(ByVal value As Boolean)
                _IsTracking = value
            End Set
        End Property

        'The Posts property is hydrated from the posts collection only when first accessed. If Record = -1 Then it is
        'initialized as a Generic.List

        <XmlArray("Posts"), XmlArrayItem("Post")> _
        Public Property Posts() As Generic.List(Of PostInfo)
            Get
                If _Posts Is Nothing Then
                    _Posts = New Generic.List(Of PostInfo)
                    _Posts.Add(New PostInfo())
                End If
                Return _Posts
            End Get
            Set(ByVal value As Generic.List(Of PostInfo))
                _Posts = value
            End Set
        End Property

        'The following properties are filled from the first (0) PostInfo record for this request
        <XmlIgnore()> _
        Public Property Body() As String
            Get
                Return Posts(0).Body
            End Get
            Set(ByVal value As String)
                Posts(0).Body = value
            End Set
        End Property

        <XmlIgnore()> _
        Public ReadOnly Property CreatedByUserID() As Integer
            Get
                Return Posts(0).CreatedByUserID
            End Get
        End Property

        <XmlIgnore()> _
        Public Property CreatedByUser() As String
            Get
                Return Posts(0).CreatedByUser
            End Get
            Set(ByVal value As String)
                Posts(0).CreatedByUser = value
            End Set
        End Property

        <XmlIgnore()> _
        Public Property Signature() As String
            Get
                Return Posts(0).Signature
            End Get
            Set(ByVal value As String)
                Posts(0).Signature = value
            End Set
        End Property

        <XmlIgnore()> _
        Public ReadOnly Property CreatedOnDate() As DateTime
            Get
                Return Posts(0).CreatedOnDate
            End Get
            'Set(ByVal value As DateTime)
            '    Posts(0).CreatedOnDate = value
            'End Set
        End Property

        <XmlIgnore()> _
        Public ReadOnly Property LastModifiedByUserID() As Integer
            Get
                Return Posts(0).LastModifiedByUserID
            End Get
        End Property

        <XmlIgnore()> _
        Public Property LastModifiedByUser() As String
            Get
                Return Posts(0).LastModifiedByUser
            End Get
            Set(ByVal value As String)
                Posts(0).LastModifiedByUser = value
            End Set
        End Property

        <XmlIgnore()> _
        Public ReadOnly Property LastModifiedOnDate() As DateTime
            Get
                Return Posts(0).LastModifiedOnDate
            End Get
        End Property

        <XmlIgnore()> _
        Public Property IsApproved() As Boolean
            Get
                Return Posts(0).IsApproved
            End Get
            Set(ByVal value As Boolean)
                Posts(0).IsApproved = value
            End Set
        End Property

        <XmlIgnore()> _
        Public Property NotifySubject() As Boolean
            Get
                Return Posts(0).NotifySubject
            End Get
            Set(ByVal value As Boolean)
                Posts(0).NotifySubject = value
            End Set
        End Property

        <XmlIgnore()> _
        Public Property Track() As Integer
            Get
                Return Posts(0).Track
            End Get
            Set(ByVal value As Integer)
                Posts(0).Track = value
            End Set
        End Property

        <XmlIgnore()> _
        Public Property ApprovedOnDate() As DateTime
            Get
                Return Posts(0).ApprovedOnDate
            End Get
            Set(ByVal value As DateTime)
                Posts(0).ApprovedOnDate = value
            End Set
        End Property

        <XmlIgnore()> _
        Public Property SubmittedFor() As String
            Get
                Return Posts(0).SubmittedFor
            End Get
            Set(ByVal value As String)
                Posts(0).SubmittedFor = value
            End Set
        End Property

        <XmlIgnore()> _
        Public Property RelationshipID() As Integer
            Get
                Return Posts(0).RelationshipID
            End Get
            Set(ByVal value As Integer)
                Posts(0).RelationshipID = value
            End Set
        End Property

        <XmlIgnore()> _
        Public Property Relationship() As String
            Get
                Return Posts(0).Relationship
            End Get
            Set(ByVal value As String)
                Posts(0).Relationship = value
            End Set
        End Property

        <XmlIgnore()> _
        Public Property Distribution() As Integer
            Get
                Return Posts(0).Distribution
            End Get
            Set(ByVal value As Integer)
                Posts(0).Distribution = value
            End Set
        End Property

        <XmlIgnore()> _
        Public Property RemoteAddr() As String
            Get
                Return Posts(0).RemoteAddr
            End Get
            Set(ByVal value As String)
                Posts(0).RemoteAddr = value
            End Set
        End Property

#End Region

#Region "Constructors"

        Public Sub New()

        End Sub
#End Region

#Region "IHydratable Implementation"

        Public Sub Fill(ByVal dr As System.Data.IDataReader) Implements DotNetNuke.Entities.Modules.IHydratable.Fill
            'From Request
            _ModuleID = Convert.ToInt32(dr.Item("ModuleID"))
            _RequestID = Convert.ToInt32(dr.Item("RequestID"))
            _IsPraise = Convert.ToBoolean(dr.Item("IsPraise"))
            _CategoryID = Convert.ToInt32(dr.Item("CategoryID"))
            _Category = Null.SetNullString(dr.Item("Category"))
            _Subject = Null.SetNullString(dr.Item("Subject"))

            Dim SC As New SubjectContactInfo(dr)
            If Not SC.IsNull Then _SubjectContact = SC

            _ChurchRelationshipID = Null.SetNullInteger(dr.Item("SubjectChurchRelationShipID"))
            _ChurchRelationship = Null.SetNullString(dr.Item("SubjectChurchRelationship"))
            _Views = Convert.ToInt32(dr.Item("Views"))
            _Updates = Convert.ToInt32(dr.Item("Updates"))
            _LastPostID = Null.SetNullInteger(dr.Item("LastPostID"))
            _LastPostDate = Null.SetNullDateTime(dr.Item("LastPostDate"))
            _StatusID = Convert.ToInt32(dr.Item("StatusID"))
            _Expires = Null.SetNullDateTime(dr.Item("Expires"))
            _Status = Null.SetNullString(dr.Item("Status"))
            _IsLocked = Convert.ToBoolean(dr.Item("IsLocked"))
            _IsTracking = Convert.ToBoolean(dr.Item("IsTracking"))

            'From first (original) Post

            Posts(0).FillInternal(dr)
        End Sub

        Public Property KeyID As Integer Implements DotNetNuke.Entities.Modules.IHydratable.KeyID
            Get
                Return _RequestID
            End Get
            Set(ByVal value As Integer)
                _RequestID = value
            End Set
        End Property
#End Region

    End Class
End Namespace
